//var angular = require('angular');//index.js中已引过,angular是全局的，这里可以省略不写)
//定义一个angular模块,引入依赖ui.router,oc.lazyLoad
var app = angular.module('app', ['ui.router','pascalprecht.translate','ngFileUpload','ngCookies','ngDialog','ngNotify','LocalStorageModule','ngTable','base64']);
module.exports = app;

//app.constant("startTime", new Date().toLocaleString());//定义全局变量的方法

app.run(['$rootScope', '$location' , '$state',  function($rootScope, $location, $state){
    //监听路由事件
    $rootScope.$on('$viewContentLoaded',function(event){
        //计算高度
        layoutCenterH();
        //左侧点击切换CSS
        $(".left_menu_ul a").off().on("click", function(){
           $(".left_menu_ul").find(".active").removeClass("active");
           $(this).parent().addClass("active");
        });
    });
}]);

//config translate to load static file 国际化配置
app.config(function($translateProvider){
	$translateProvider.useStaticFilesLoader({
    	files: [{
    	  prefix: 'i18n/locale-',
          suffix: '.json'
        }]
    });
    $translateProvider.useLocalStorage();
    //set preferred lang
    $translateProvider.preferredLanguage('cn');
    //$translateProvider.preferredLanguage('en');
});

app.config(function (localStorageServiceProvider) {
    localStorageServiceProvider.setPrefix('graphone');//前缀
    //localStorageServiceProvider.setStorageType('sessionStorage');
    //localStorageServiceProvider.setStorageCookieDomain('example.com');
});

app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
    //嵌套路由配置
    $urlRouterProvider
        .when('','/home/graph')
        .when('/','/home/graph')
        .when('/home','/home/graph')
        .when('/home/recovery','/home/recovery/backup')
        .when('/home/devhelp','/home/devhelp/i18ndemo')
        .otherwise('/home/404');
    $stateProvider
        .state('login',{
            url: '/login',
            templateUrl : 'templates/layout/login.html',
            controller: 'loginController'
        })
    	//在app路由上定义默认的内容区块
        .state('app',{
            url: '/home',
            views: {
                '':{
                    templateUrl : 'templates/layout/home.html',
                    controller: 'homeController'
                },
                'header@app': {
                    templateUrl: 'templates/layout/header.html',
                    controller: 'headerController'
                },
                'content@app': {
                    templateUrl: 'templates/layout/content.html'
                },
                'footer@app': {
                    templateUrl: 'templates/layout/footer.html',
                    controller: 'footerController',
                }
            }
        })
        .state('app.graph',{
            url: '/graph',
            views: {
                'content@app': {
                    templateUrl: 'templates/graph/graph.html',
                    controller: 'graphController'
                }
            }
        })
        //备份恢复 页面路由
        .state('app.recovery', {
            url: '/recovery',
            views: {
                'content@app': {
                    templateUrl: 'templates/recovery/recovery.html',
                    controller: 'recoveryController'
                }
            }

        })
        //备份恢复 备份 页面路由
        .state('app.recovery.backup', {
            url: '/backup',
            views: {
                'detail@app.recovery': {
                    templateUrl: 'templates/recovery/backup.html',
                    controller: 'backupController'
                }
            }

        })
        //备份恢复 恢复 页面路由
        .state('app.recovery.restore', {
            url: '/restore',
            views: {
                'detail@app.recovery': {
                    templateUrl: 'templates/recovery/restore.html',
                    controller: 'restoreController'
                }
            }

        })	
	    //内容页面
	    .state('subscribers', {
        	url: '/subscribers',
        	views: {
            	'content@app': {
                	templateUrl: 'templates/subscribers/subscribers.html',
                	controller: 'SubscriberController'      
            	}
			}
    	})
    	//根据ID来跳转到相应的页面
    	.state('subscribers.detail', {
        	url: '/:id',
	        /*
	        templateUrl: 'templates/partials/subscriber-detail.html',
	        controller: 'SubscriberDetailController'
	        */
        	views: {
            	'detail@app.subscribers': {
                	templateUrl: 'templates/subscribers/subscriber-detail.html',
                	controller: 'SubscriberDetailController'        
            	}
        	}
    	})
		//内容页面  devhelp
	    .state('app.devhelp', {
        	url: '/devhelp',
        	views: {
            	'content@app': {
                	templateUrl: 'templates/devhelp/devhelp.html',
                	controller: 'devhelpController'      
            	}
			}
    	})
    	//devhelp i18n demo
    	.state('app.devhelp.i18ndemo', {
        	url: '/i18ndemo',
        	views: {
            	'detail@app.devhelp': {
                	templateUrl: 'templates/devhelp/i18ndemo.html',
                	controller: 'i18ndemoController'        
            	}
        	}
    	})
		//devhelp echarts demo
    	.state('app.devhelp.echarts', {
        	url: '/echarts',
        	views: {
            	'detail@app.devhelp': {
                	templateUrl: 'templates/devhelp/echarts.html',
                	controller: 'echartsController'        
            	}
        	}
    	})
    	//devhelp alertmodel demo
    	.state('app.devhelp.alertmodel', {
        	url: '/alertmodel',
        	views: {
            	'detail@app.devhelp': {
                	templateUrl: 'templates/devhelp/alertmodel.html',
                	controller: 'alertmodelController'        
            	}
        	}
    	})
        //devhelp codemirror demo 代码编辑器
        .state('app.devhelp.codemirror', {
            url: '/codemirror',
            views: {
                'detail@app.devhelp': {
                    templateUrl: 'templates/devhelp/codemirror.html',
                    controller: 'codemirrorController'        
                }
            }
        })
        //devhelp d3
        .state('app.devhelp.d3', {
            url: '/d3',
            views: {
                'detail@app.devhelp': {
                    templateUrl: 'templates/devhelp/d3.html',
                    controller: 'd3Controller'        
                }
            }
        })
        //devhelp fontawesome
        .state('app.devhelp.fontawesome', {
            url: '/fontawesome',
            views: {
                'detail@app.devhelp': {
                    templateUrl: 'templates/devhelp/fontawesome.html',
                    controller: 'fontawesomeController'        
                }
            }
        })
    	//devhelp button
    	.state('app.devhelp.commonbutton', {
        	url: '/commonbutton',
        	views: {
            	'detail@app.devhelp': {
                	templateUrl: 'templates/devhelp/commonbutton.html',
                	controller: 'commonbtnController'
            	}
        	}
    	})
        //devhelp button
        .state('app.devhelp.ngtable', {
            url: '/ngtable',
            views: {
                'detail@app.devhelp': {
                    templateUrl: 'templates/devhelp/ngtable.html',
                    controller: 'ngtableController'
                }
            }
        })
        //404
        .state('app.404', {
            url: '/404',
            views: {
                'content@app': {
                    templateUrl: 'templates/layout/404.html',
                    controller: '404Controller'
                }
            }
        });    
}]);

//layout
function layoutCenterH(){
    //初始化layout_center的高度
    var layout_center_h = $(window).height() - $(".layout_top").height() - $(".footer").height();
    //设置最小高度是200
    if( layout_center_h < 200 ){
        layout_center_h = 200;
    }
    $(".layout_center").height(layout_center_h);
};
$(window).resize(function(){
    layoutCenterH();
});